package com.geekstudy.week8.config;




import com.atomikos.datasource.pool.ConnectionPool;
import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import javax.transaction.SystemException;
import java.io.PrintWriter;
import java.sql.SQLException;

/**
 *
 *
 * https://developer.aliyun.com/article/770674
 *
 * https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/usage/sharding/spring-boot-starter/
 *
 *
 * https://mvnrepository.com/artifact/org.apache.shardingsphere/shardingsphere-jdbc-spring-boot-starter-infra
 * https://mvnrepository.com/artifact/org.apache.shardingsphere/sharding-jdbc-spring-boot-starter/4.1.1
 *
 */
@Configuration
public class ShardingSphereConfig {

    //@Autowired
    //private DataSource dataSource;

    @Autowired
    private ShardingSphereDataSource shardingSphereDataSource;

    /*
    @Bean
    public AtomikosDataSourceBean atomikosDataSourceBean() throws SQLException {
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();

        //<dependency>
		//	<groupId>mysql</groupId>
		//	<artifactId>mysql-connector-java</artifactId>
		//	<version>8.0.16</version>
		//</dependency>
        // 5.0前版本的是: driverClassName: com.mysql.jdbc.Driver
        // 6.0后版本是: driverClassName: com.mysql.cj.jdbc.Driver
        // https://blog.csdn.net/qq_40206199/article/details/103256158
        // com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
        // com.mysql.cj.jdbc.MysqlXADataSource
        atomikosDataSourceBean.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource");

        // com.atomikos.jdbc.internal.AtomikosSQLException: Property 'uniqueResourceName' cannot be null
        atomikosDataSourceBean.setUniqueResourceName("demo2021");

        // The following method did not exist:
        // com.atomikos.datasource.pool.ConnectionPool.borrowConnection()Ljava/lang/Object;
        // atomikosDataSourceBean.setXaDataSource();

        atomikosDataSourceBean.setPoolSize(10);
        atomikosDataSourceBean.setMinPoolSize(3);
        atomikosDataSourceBean.setMaxPoolSize(30);
        atomikosDataSourceBean.setBorrowConnectionTimeout(60);
        atomikosDataSourceBean.setReapTimeout(20);
        atomikosDataSourceBean.setMaxIdleTime(1800);
        atomikosDataSourceBean.setMaintenanceInterval(60);
        atomikosDataSourceBean.setLoginTimeout(60);
        // atomikosDataSourceBean.setLogWriter(new PrintWriter(60));
        atomikosDataSourceBean.setTestQuery("select 1");

        return atomikosDataSourceBean;
    }

    @Primary
    @Bean("userTransactionManager")
    public UserTransactionManager userTransactionManager() {
        UserTransactionManager userTransactionManager = new UserTransactionManager();
        userTransactionManager.setForceShutdown(true);

        return userTransactionManager;
    }

    @Bean("userTransactionImp")
    public UserTransactionImp userTransactionImp() throws SystemException {
        UserTransactionImp userTransactionImp = new UserTransactionImp();
        userTransactionImp.setTransactionTimeout(300);
        return userTransactionImp;
    }
    */




}
